/*************************************************************************
	> File Name: filesize.c
	> Author: Angel Criss
	> Mail: abbytsing@gmail.com 
	> Created Time: 六  3/29 18:20:13 2014
 ************************************************************************/

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
int main(int argc, char * argv[])
{
    off_t len;
    double big;
    mode_t per;
    char *error_file_name[argc];
    int i=1,j=0;
    struct stat size;

    struct stat mode;
    lstat(argv[1],&mode);
    per=size.st_mode;
    if(argc<2)
    {
        printf("run by: ./a.out filename\n");
        exit(0);
    }
    int fileo;
    while(argv[i]!=NULL)
    {
      if((fileo=open(argv[i],O_RDWR|O_APPEND,per))==-1)
      {
        error_file_name[j]=argv[i];
        j++;
      }
      else
        {
            fstat(fileo,&size);
            len=size.st_size;
            big=(double)len/1024;
            printf("the real length of \e[36m%s\e[0m is: %10.10f KB\n",argv[i],big);
        }
        i++;
    }
    printf("the files that cannot be open are %d\n",j);
    while(j--)
    {
        printf("## %s##\n", error_file_name[j]);
    }
    if((close(fileo)==-1))
            perror("close failed");
    else
            printf("all done\n\n");
    exit(0);
}
